package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {

    @Autowired
    private IWorkerCertificationService workerCertificationService;

    //服务人员实名认证
    @Override
    public void workerCer(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {

        WorkerCertificationAudit workerCertificationAudit = BeanUtil.toBean(workerCertificationAuditAddReqDTO, WorkerCertificationAudit.class);
        Long currentUserId = UserContext.currentUserId();
        workerCertificationAudit.setAuditStatus(0);
        this.baseMapper.insert(workerCertificationAudit);
        Long serveProviderId = workerCertificationAuditAddReqDTO.getServeProviderId();
        //在worker_certification表中查询服务人员信息
        WorkerCertification workerCertification = workerCertificationService.getById(serveProviderId);
        if (ObjectUtil.isNotNull(workerCertification)) {
            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertification.setId(currentUserId);
            workerCertificationService.updateById(workerCertification);
        } else {
            WorkerCertification workerCertification1 = BeanUtil.toBean(workerCertificationAuditAddReqDTO, WorkerCertification.class);
            workerCertification1.setId(serveProviderId);
            workerCertification1.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertificationService.save(workerCertification1);
        }

    }

    //查询最新的驳回原因
    @Override
    public RejectReasonResDTO rejectReason() {
        //1.查询新的驳回原因
        Long currentUserId = UserContext.currentUserId();
        LambdaUpdateWrapper<WorkerCertificationAudit> auditLambdaUpdateWrapper = Wrappers.<WorkerCertificationAudit>lambdaUpdate()
                                                                                         .eq(WorkerCertificationAudit::getServeProviderId, currentUserId);
        WorkerCertificationAudit workerCertificationAudit = this.baseMapper.selectOne(auditLambdaUpdateWrapper);
        return new RejectReasonResDTO(workerCertificationAudit.getRejectReason());
    }

    /**
     * 服务人员分页查询
     *
     * @param workerCertificationAuditPageQueryReqDTO
     * @return
     */
    @Override

    public PageResult<WorkerCertificationAuditResDTO> pageQueryWorkerCertificationAudit(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryReqDTO) {
        return PageHelperUtils.selectPage(workerCertificationAuditPageQueryReqDTO,
                () -> baseMapper.pageQueryWorkerCertificationAudit(workerCertificationAuditPageQueryReqDTO));
    }

    @Override
    @Transactional
    public void audit(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {
        // 查询服务人员认证审核信息
        WorkerCertificationAudit workerCertificationAudit = getById(id);
        if (workerCertificationAudit == null) {
            throw new RuntimeException("服务人员认证审核信息不存在");
        }
        Long serveProviderId = workerCertificationAudit.getServeProviderId();
        WorkerCertification workerCertification = workerCertificationService.getById(serveProviderId);
        // 获取当前登录用户信息
        Long currentUserId = UserContext.currentUserId();
        String currentUserName = UserContext.currentUser().getName();

        // 更新审核状态和认证状态
        workerCertificationAudit.setAuditStatus(1); // 已审核
        workerCertificationAudit.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        workerCertification.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        workerCertificationAudit.setAuditTime(LocalDateTime.now());
        workerCertificationAudit.setAuditorId(currentUserId);
        workerCertificationAudit.setAuditorName(currentUserName);

        // 如果认证失败，设置驳回原因
        if (certificationAuditReqDTO.getCertificationStatus() == 3) {
            workerCertificationAudit.setRejectReason(certificationAuditReqDTO.getRejectReason());
        }

        // 更新数据库
        updateById(workerCertificationAudit);
        workerCertificationService.updateById(workerCertification);
    }


}
